package invest;

import invest.data.DataEnum;
import invest.data.DataFactory;
import invest.mapper.FinanceMapper;
import invest.mapper.RecordDao;
import invest.mapper.StockHistoryModelMapper;
import invest.mapper.StockHistoryMapper;
import invest.model.Record;
import invest.model.StockHistoryModel;
import invest.pojo.datapojo.KlineHistory;
import invest.pojo.datapojo.Stock;
import invest.pojo.datapojo.StockMinuteToday;
import invest.utils.Suanfa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import invest.utils.DateUtil;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;


@Component
public class Analyze implements CommandLineRunner {
    @Autowired
    RecordDao recordDao;
    @Autowired
    StockHistoryModelMapper stockHistoryModelMapper;
    @Override
    public void run(String... args) throws Exception {
        Date startDate = new Date(115, 05, 19);
        Date endDate = new Date(123, 07, 10);
        List<Date> dateList = stockHistoryModelMapper.getDistinctDate();
        for (int i = 0; i < dateList.size()-2; i++) {
            startDate = dateList.get(i);
            Date nextDate = DateUtil.getDayAfter(startDate, i+1);
            Date nextNextDate = DateUtil.getDate(startDate, i+2);
            List<StockHistoryModel> stockHistoryModelList=stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(startDate));
            List<StockHistoryModel> nextDayStockList = stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(nextDate));
            Map<String, StockHistoryModel> nextDayMap = new HashMap<>();
            for (StockHistoryModel s : nextDayStockList) {
                nextDayMap.put(s.getCode(), s);
            }
            List<StockHistoryModel> nextNextDayStockList = stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(nextNextDate));
            Map<String, StockHistoryModel> nextNextDayMap = new HashMap<>();
            for (StockHistoryModel s : nextNextDayStockList) {
                nextNextDayMap.put(s.getCode(), s);
            }
            for (StockHistoryModel s : stockHistoryModelList) {
                if (!nextDayMap.containsKey(s.getCode()) || !nextNextDayMap.containsKey(s.getCode())) {
                    continue;
                }
                Double nextDayOpening = nextDayMap.get(s.getCode()).getOpeningPrice();
                Double nextDayClosing = nextDayMap.get(s.getCode()).getClosingPrice();
                if ((nextDayClosing - nextDayOpening) / nextDayOpening > 0.06 &&(nextDayClosing - nextDayOpening) / nextDayOpening < 0.098 && (nextDayOpening-s.getClosingPrice())/s.getClosingPrice()<-0.03) {
                    Double profitRange = (nextNextDayMap.get(s.getCode()).getClosingPrice() - nextDayClosing) / nextDayClosing;
                    Record record = new Record();
                    record.setCode(s.getCode());
                    record.setDate(nextDayMap.get(s.getCode()).getDate());
                    record.setName(s.getName());
                    record.setRiseRange(profitRange);
                    recordDao.insert(record);
                    Thread.sleep(50);
                }
            }
        }
        while (startDate.before(endDate)) {
            startDate = DateUtil.getDayAfter(startDate, 1);
            List<StockHistoryModel> stockHistoryModelList=stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(startDate));
            Date nextDate = DateUtil.getDayAfter(startDate, 1);
            Date nextNextDate = DateUtil.getDate(startDate, 2);
            List<StockHistoryModel> nextDayStockList = stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(nextDate));
            Map<String, StockHistoryModel> nextDayMap = new HashMap<>();
            for (StockHistoryModel s : nextDayStockList) {
                nextDayMap.put(s.getCode(), s);
            }
            List<StockHistoryModel> nextNextDayStockList = stockHistoryModelMapper.getDate(DateUtil.getStringDateToDat(nextNextDate));
            Map<String, StockHistoryModel> nextNextDayMap = new HashMap<>();
            for (StockHistoryModel s : nextNextDayStockList) {
                nextNextDayMap.put(s.getCode(), s);
            }
            for (StockHistoryModel s : stockHistoryModelList) {
                Double nextDayOpening = nextDayMap.get(s.getCode()).getOpeningPrice();
                Double nextDayClosing = nextDayMap.get(s.getCode()).getClosingPrice();
                if ((nextDayOpening - s.getClosingPrice()) / s.getClosingPrice() > 0.099 && (nextDayClosing-nextDayOpening)/nextDayOpening<-0.01) {
                    Double profitRange = (nextNextDayMap.get(s.getCode()).getOpeningPrice() - nextDayClosing) / nextDayClosing;
                    Record record = new Record();
                    record.setCode(s.getCode());
                    record.setDate(nextDayMap.get(s.getCode()).getDate());
                    record.setName(s.getName());
                    record.setRiseRange(profitRange);
                    recordDao.insert(record);
                    Thread.sleep(50);
                }
            }
        }
        System.out.println("finish");
    }

    private Double getMv5(List<KlineHistory> klineHistories, int i) {
        Double m5 = (klineHistories.get(i).getClose() + klineHistories.get(i - 1).getClose() + klineHistories.get(i - 2).getClose() + klineHistories.get(i - 3).getClose() + klineHistories.get(i - 4).getClose()) / 5;
        return m5;
    }

}



